#include "FileTest.h"
#include <stdio.h>
#include <string.h>

/*
 * Created: 2/24/12; CGP
Part1: Create file containing 10,000 bytes

Part2:
a) Open an existing file for reading that contains 10,000 bytes of data, 
    call this SOURCE. At least two different bytes should be contained in 
    this file (e.g., don't write just all 0 values into the file).
b) Open a new file, for writing, call this DEST.
c) Copy all bytes of data from SOURCE to dest using your read and write methods. 
    Use a buffer that is 100 bytes long in your reads and writes.
d) Close both files.
e) Make sure that the directory listing indicates that the new file has been created 
    and is of the correct length.
f) Open the DEST file for reading and verify that the contents are correct.
 */

#define NUMBER_WRITES 10000

// Usage: Part1: No command line arg
///    Part1: with a command line argument
int main(int argc, char *argv[]) {
    int length;
    printf("Starting test...\n"); fflush(stdout);
    FileTest ft;
    const char *fileName1 = "SOURCE";
    const char *fileName2 = "DEST";
    unsigned char data[] = "abcdefghijklmnopqrstuvwxyz";
    File *f1, *f2;

    if (argc == 1) {
        printf("Writing to file...\n");
        f1 = new File(fileName1, "w");

        for (int i=0; i < NUMBER_WRITES; i++) {
            length = f1->Write(data + (i % 10), 1);
        }
        if (! f1->Close()) {
            printf("ERROR: Could not close file\n");
        }
        delete f1;
    } else {
        printf("Reading & writing files...\n");
        f1 = new File(fileName1, "r");
        f2 = new File(fileName2, "w");
        unsigned char buffer;
        for (int i=0; i < NUMBER_WRITES; i++) {
            length = f1->Read(&buffer, 1);
            
            if (length != 1) {
                printf("ERROR: Could not read back all bytes of file\n");
            }
            
            if (data[(i % 10)] != buffer) {
                printf("ERROR: Buffer is not equal to data written (expected= %c, actual= %c)\n",
                    data[i], buffer);
            }

            length = f2->Write(&buffer, 1);
            if (length != 1) {
                printf("ERROR: Could not write to file\n");
            }
        }
        
        if (! f1->Close()) {
            printf("ERROR: Could not close file\n");
        }
        if (! f2->Close()) {
            printf("ERROR: Could not close file\n");
        }
        delete f1;
        delete f2;

        if (ft.dir->LookUp("DEST") == NULL) {
            printf("ERROR: DEST file is not present in directory\n");
        }

        f1 = new File(fileName2, "r");
        for (int i=0; i < NUMBER_WRITES; i++) { 
            length = f1->Read(&buffer, 1);
            
            if (length != 1) {
                printf("ERROR: Could not read back all bytes of file\n");
            }
            
            if (data[(i % 10)] != buffer) {
                printf("ERROR: Buffer is not equal to data written (expected= %c, actual= %c)\n",
                    data[i], buffer);
            }
        }

        if (! f1->Close()) {
            printf("ERROR: Could not close file\n");
        }

        delete f1;
    }
}





